#include <CheckButton.h>

// Scratchboard emulation library by Kevin Osborn
//https://github.com/osbock/ScratchSensors
//http://baldwisdom.com
#include <ScratchSensors.h>
ScratchSensors Scratchboard;



#include <Esplora.h>
int x_ofset = 0;
int x_value = 0;
int y_ofset = 0;
int y_value = 0;

void setup()
{
//  //Initialize serial and wait for port to open:
//  Serial.begin(9600); 
//  while (!Serial) {
//    ; // wait for serial port to connect. Needed for Leonardo only
//  }
  x_ofset = Esplora.readJoystickX();
  delay(100);
  y_ofset = Esplora.readJoystickY();
  delay(100);
//  //Initialize the Scratchboard library
//  Scratchboard.init();
}

enum {
  ST_IDLE = 0,
  ST_RUNNING = 1,
  ST_DEBUG = 2
};

CheckButton cb_down(SWITCH_DOWN);
CheckButton cb_up(SWITCH_UP);
int workStatus = ST_IDLE;

void loop()
{
  switch (workStatus)
  {
    case ST_IDLE:
      if (cb_down.check() == CB_DCLICK) {

        //Initialize the Scratchboard library
        Scratchboard.init();    
    
        workStatus = ST_RUNNING;
        Esplora.writeGreen(10);    
      } else if (cb_up.check() == CB_DCLICK) {  
    
        workStatus = ST_DEBUG;
        Serial.begin(9600);
        Esplora.writeRed(10);  
        
      }      
      break;
      
    case ST_RUNNING:
      if (cb_down.check() == CB_DCLICK) {
        workStatus = ST_IDLE;
        Esplora.writeRGB(0,0,0);
        Serial.end(); 
      } else {
        manageBoard(false);
      }
      break;
      
    case ST_DEBUG:
      if (cb_up.check() == CB_DCLICK) {
        workStatus = ST_IDLE;
        Esplora.writeRGB(0,0,0);
        Serial.end(); 
      } else {
        manageBoard(true);
      }
      break;      
  }
}

void manageBoard(bool debug)
{
  Scratchboard.Values[SLIDER] = Esplora.readSlider();
  Scratchboard.Values[LIGHT] = Esplora.readLightSensor();
  Scratchboard.Values[SOUND] = Esplora.readMicrophone();
  Scratchboard.Values[BUTTON] = Esplora.readButton(SWITCH_DOWN) == HIGH?1023:0;
  
  //Map the resistance A and B to accelerometer
  x_value = Esplora.readAccelerometer(X_AXIS);
  Scratchboard.Values[RESISTA] = map(x_value, -180, 180, 0, 100);
  
  y_value = Esplora.readAccelerometer(Y_AXIS);
  Scratchboard.Values[RESISTB] = map(y_value, -180, 180, 0, 100);
  
  //Map the resistance C and D to the joystick
  //Check to see if the X axis is nearly at the centre if so report zero.
  x_value = Esplora.readJoystickX()-x_ofset;
//  if(x_value == 1 or x_value == -1){
//    Scratchboard.Values[RESISTC] = 0;
//  }else{
//    Scratchboard.Values[RESISTC] = x_value;
//  }

  if (abs(x_value)<10) x_value = 0;
  x_value = map(x_value, -512, 512, 0, 100);
  Scratchboard.Values[RESISTC] = x_value;
  if (debug) { Serial.print("x_value: "); Serial.println(x_value); };


  //Check to see if the Y axis is nearly at the centre if so report zero.
  y_value = Esplora.readJoystickY()-y_ofset;
//  if(y_value == 1 or y_value == -1){
//    Scratchboard.Values[RESISTD] = 0;
//  }else{
//    Scratchboard.Values[RESISTD] = y_value;
//  }
  
  if (abs(y_value)<10) y_value = 0;
  y_value = map(y_value, -512, 512, 0, 100);
  Scratchboard.Values[RESISTD] = y_value;
  if (debug) { Serial.print("y_value: "); Serial.println(y_value); };
  
  //Send the reading to Scratch
  if (!debug) Scratchboard.report(); 
}

  


